{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Simplification"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sympy import *\n",
    "x, y, z = symbols('x y z')\n",
    "init_printing(use_unicode=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Simplify\n",
    "\n",
    "化简表达式的函数: `simplify()`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAAgAAAAOCAYAAAASVl2WAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAAXUlEQVQYGWP8//8/Aww0NDQYAdmrgdgYyP4AEmcBMgSA9GwgfgfEJkCsBMRwAFIAUhkKEgGyy4AUyBQ4YIKzcDBGFUAChmA4MIIiCxjEoAgCxQkoLkD0PSA+B8SnAeDgGGsGGU5UAAAAAElFTkSuQmCC\n",
      "text/latex": [
       "$\\displaystyle 1$"
      ],
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "simplify(sin(x)**2 + cos(x)**2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAI0AAAAwCAYAAAAsCvkOAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAGMElEQVR4Ae2c63HUMBDHHSYF8OggdMCjAkIHMFQAdACTT8k3hnQAVMCjA6ACAh0AFRCug/D/GUnjE47PlqWzdGhndJIla7W7Wu+uZOv2Li4umqlwcnJyoD4PlFZKV5XuKr1Q/TflWYNohN4jQyR8AI9VDy8VRkhgf8Q9fbe8VOUPCfqURuXPlH1WusZ15vBS9D61NKr8SuWvSjdtXc2HJXBluPnS1hdqedtpvaHyj851zsUnUpTDDoE8AAequ9Wpq8UBC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cGMQqD1XMfsavMCQ2+JXFKPYBi0SbRiJvvnsG2rj46XYsrjZj1z2+jRPxh0uSnW30O1Tf0TDgKwgFBB8KHleF4D09t8vNSc+hXXz4taRVeZc6tYW2SQFarJzELo3yQFfrVP08bKQRQuN+iwe+P2+TD+mRPbofYOfR30KQtZqM0ZlJ42pP44RFiRDmwcKtL7vWV6ZLbdr86iXsyT+vYc+GNVRjli/2Lg8Z2sYw37a2ZV7tzlyqjQKP58/AVf5nK0hBQnpIkIU4yEKAd6pqVCQJ3R4BVh9lnmZ3dvziINhQG+uyKSsUWRvNnO+xSHt3SSNCXnQu3gr8uATqlUTmrf3HwJpcA+LV4cic/A/jzUJZ/GV1pJJJJ55M1CZNXJeqDZSJw9QGFbNTed/x305nwNVxmjE89uCbxt4bUXBjcSenvGzdWXXSlkUCc7zdEIhxrZaLQ3TORLV7VRzkKIzxtvNI3jupm89eHFwZUH4X+KEIeQJIqpmmHlBDs0+QO26ku63PhZuJuKnfWSmX2foht1kB1xfG3xkDgRVRLIyES5BIHECiyhG0Fr3L258KRn+gk8L2r3LeMWABim6L5g8cYEFVpRBBPHomdVAR8rrRSakF1tLGayg5EG5YEhSeOIWayQJzEf/OxGkR5iuTPMhMj34+BpIMD61LquXAUBsWx+y8dthobx2TNn5QaHnhY7xji185gdxmaU178Lfcc4v2+xhIE/TmSj2uJ61LoTxoILyB4XKFzhwuMP3fIIuj/A3Nk11N7QLfgAAAAAElFTkSuQmCC\n",
      "text/latex": [
       "$\\displaystyle \\frac{x^{3} + x^{2} - x - 1}{x^{2} + 2 x + 1}$"
      ],
      "text/plain": [
       " 3    2        \n",
       "x  + x  - x - 1\n",
       "───────────────\n",
       "   2           \n",
       "  x  + 2⋅x + 1 "
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "simplify(x**3 + x**2 - x - 1)/(x**2 + 2*x + 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "out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PmcrV7iUiHQK38xD5CyDkHiMkf3vlPReFiqLknM+/iqJQTPxuJWkxLFzmTKNflVYGPqXshweOB9QYhDDA4h2oggnNwL1TOVSBVm+Dz+o3R4ByOyIcDwLIHx7iF+L8yzChvTWYsnVhoWzQX4AEwKUqyp0Kx5Y8A3GCgaHR/jDDve+4gybp9dCMp6FwqLdL9FZcqPFAvtjPCYeEKKiTZw8bywVOUlJ1Ad7x4wFNrZ4OOdrNbC992XKAZTqar/KsctKAk17xooFxqZ4hMhnjA821tnPWK2EPdkPUYZxY8QCM9fwaP+ThxG/Mo5UF0cK84ZIsoGEkBtByRWvY8KYsMA4C48/VLsVpZ9FndAAAAAASUVORK5CYII=\n",
      "text/latex": [
       "$\\displaystyle \\left(x - 2\\right) \\left(x - 1\\right)$"
      ],
      "text/plain": [
       "(x - 2)⋅(x - 1)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "simplify(gamma(x)/gamma(x - 2))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`gamma(x)` is $\\Gamma{(x)}$. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "化简 $x^2 + 2x + 1$ 成 $(x+1)^2$ 使用的不是 `simplify()` 函数而是 `factor()` 函数, 即因式分解"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAEYAAAAaCAYAAAAKYioIAAAACXBIWXMAAA7EAAAOxAGVKw4bAAADAklEQVRYCeWY21EbMRSGDZMCICU4HZhQQUgHQCoAd4CHJ/uVdJCkhNCBSQVcOjAlEHfgfN9G2pHXK+xlxsaXM3Ms6ej6H52L1nuTyaS1qzQYDA7Afh3wt0N5gXz8YVeVEnDfoIRu1AH1H9Qf4U/7Ubij5SXKOEmw31BvI+vsumK0lodEMWV1b5djTKmFUMFStJhTyllXQhiDUHXeRrWb4mB8R6XARwKdciU6r5A5YBvIWCGeuRSUqLUcUR87oXQlBGrrmLJnxyYQZ/USf8MloPTc9KuYZ8rbVJ7W6dNDzE5nyildc1wohob5/A9lYUYOWFcKZ/3F+V7gz7BADpEXN019ipCbfr/U9SNTKabo1BgMyL34jtGMHLAy4lCmSc39Z5NNA8B4u4u4vrjEV75Xkv1UmkZhWRJ7dKNizm2UPaupeCB5qaTi4b9wD56yKtqHuc336fTmnnMDtkQuvvMmWLQYzfIuNwnFeauXof+Y8gLWN78F2T1jssEtjHnvQnxf4YXd1nRtABvBOTJif5cZcA8b+E5oG7BUmv677iQ+L3NhUjGCM8LPEOC1lCpw03q0kI81/TPrrIFAfOJcmHQlwY0zMx5QThp/NMenKKMsskNmbiFmjFnBOFYl923RXxf03WPu2tUFX2mLoZHFqJgscbinSqcA05xf6Z5tZoC3kGt5pmtddNn02uXX7q0rqc25ZgaAeOvRjQR3INeuvF5Cz1gbLnLH1GKcMGNmAbDPbYOvUb0weeqpa13TbmRBrPMepMWk5557Bi1GdzENV0kLkV+CklRgGYuQ2WeW2gTyU2fY5KBajBPqPge0EvO+CmihiC48hB3rE1qFlW7lmFUR+2rJuodPDekRmRfsm6ouZomhml2dlyf/qOr3+yO4Y31VzH6n8NWy92OPNjxquo+uJOU+sv73LudXtyxdczlbFKsaA5tZi9OiJtHqUO3G9jaUwVqGb8ESLUYd+aCqizX2bSqJ500PxfIfPJETuEzbfgct/LHlvHUkMPg5c0fZKE1HLP8ALhqH+fWPhJ0AAAAASUVORK5CYII=\n",
      "text/latex": [
       "$\\displaystyle \\left(x + 1\\right)^{2}$"
      ],
      "text/plain": [
       "       2\n",
       "(x + 1) "
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "factor(x**2 + 2*x + 1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`simplify()` 函数还会导致计算速度慢, 如果明确知道换件之后的形式, 应该使用指定的科学计算函数来计算"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Polynomial/Rational Function Simplification\n",
    "\n",
    "#### expand\n",
    "\n",
    "`expand()` 将因式分解形式的多项式展开成各项相加的形式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAGcAAAAVCAYAAABbq/AzAAAACXBIWXMAAA7EAAAOxAGVKw4bAAADGUlEQVRoBe2Z7VEbMRCGTcYFkJQAHfDRAXRAJhUAHSTDP//LkA5CSiAdhA4MlJASiDtwnkcjaY7LmfgkG84z3hmNVnun1atdaXfP3pnP56O+NJlMdplzFeftxf4c+ayvru37iy0wXvzoxSfXOOIyvQH/Hf6Btp9k277eAu8KVVzgkJPG3Gv4PWQHDdmWrbRAqXO8NfeVa2+n/8cCOyU5p62TG+PNOaPfhrW2cSrGpTknLxlD2RmCwyx8ZQYMFiVf4rJH9E+OkT9G2eC7aMdbgB7Ch8KqyjnRKN6arPC1rZAw0H9Ma8OL6YH+lHaX5EPrwWbV+4PmYfJQpcoXdjQqdg6KVWTVdqoieouBGf1vx32IORYXFhQ3febFd3XEeXMeerw1F8g8ie+bz1bN12BnrjckHCr4z/DPCqqiggBFOsby+Sv8gQ3eIsETUEKeIFsJ6dg/YGjP98bsRqwlepedU4P9xTVKb47fNIKyz4Qh8rdPFq6f0QkekNmCpdpOW/Da8MTZOfHkGQqkY5qhwhvyiSZNeeenDP1aQ4VrLEtgybmmNSeECJ7nogBeRy21x5auNxk2w5r545sNJFOaieqEsVWQmzK2bwSBWcd4sFIFl3Bv1B6Dc9iMp6ltfMvjcFPoP3Q8RzRYshC4iQctgNzEPaawdg/4ZpVlBfaYZPSLQkcv76DHIsIE3iadP+J5V84Sx9LrxzXuOnRV7THqXSv2tlGCc1g4x+X4giDaIaE9t/e4w2BBB3JvqaW0IbWYmB/ySdc6yKr22KVToMhXgr1r082cE56zWDodKaQJwJJ00FUP+DTSPn2+ffA63NzzjJBtxB7HANXoxmiTpWVp2Bx8M8xdMV75TXpmsYoB2CwAjjsw6jBzz0bu0bDmKbL5Ve0m/JCc0QIh85nV2yAJfN4MD5d5xpyWyDzm948VqE7auD2OAe1t8WcTwY/YyCXtFy39gfYEn0Oc7wyMdIwOSt8vTXgpzwx2j9hW/F6Kowjc3wTFPV3JXwZRaXEHGE92dUFQDKBi4jqx/1MQVOCsmWoYzaG0RtEbzF0b9r+J/UWOIGFWiwAAAABJRU5ErkJggg==\n",
      "text/latex": [
       "$\\displaystyle x^{2} + 2 x + 1$"
      ],
      "text/plain": [
       " 2          \n",
       "x  + 2⋅x + 1"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "expand((x+1)**2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAF4AAAATCAYAAAAZFLrcAAAACXBIWXMAAA7EAAAOxAGVKw4bAAACcElEQVRYCe2Y0U0CQRCGwVAAWgJ0gNgBdgBaAVAC4QneDHagVKDYgViBSgfSgYYO8P/W3c3mwp3hDuMm3CTj7szsDnP/zc7OWd1ut5V9aTqd1rVnbPc17NiXfrOvr2NdX8v54DOBPHR7Nb/T/F3cdLpyzEbgJNucah0I7E5gnWnekK4V6MppBgJ5gSfb3zL8lqZfEKjmqfFJn8p0Mr6rsSw1SXBS5Lw13ruz5aUrxblXlhOPgE1KL2vyIN2qEPByQEdDtp9rXnY0AbwWm2ephpovMWlcaICbuYG3juluLq1TLtaN5DVySQbgpfAwoFs8aMMNPrlqvAWdFnIUAMyFO5Lt6DNfGFB6yezTNDzyZjw9O2+P0ZN+xPf2XnmcEz4uOf2pSegzXosAcmBxutDYF1PDr63uVWue7DyqIbbYFQ//DliJwRD8PsV0fAvZTOkJ+3jq9S2sBa/iubgjmXLCS+ESjZWiiV14gZWjNviBqZhqAPCUoYoBXgKZngSWBS7Dz3bYpfp/ijj2lmK7TyD0KHkufd3V+DcJYTdCp7JyOo29hINcIj+ojS/iMCt+89XTPo5tGhWK/dAxyR+1nVhDPF3s3IkkedsAv+PBOjKGHYvbWGgkKDk46IdW0dj/IiY9I6DzrGnUCGu8WaRAAB1yZaYiXR3+Ucf7N6LYAT4Lr3XNAkrPyQXFjWtaQs3DozKWfPATUPQVRhw79yVfrUnitFOKlmQ8GQ5/2Qf5wig2JB02upwYKcrYAVZg8dXqGxaL7ZX0tJiV6mQy4Uiw4AOFFtD60NFwwXIZ8EJ82ZEcDSmuqGNXfODqSg6d4Y10plH4BizlFuajDy2qAAAAAElFTkSuQmCC\n",
      "text/latex": [
       "$\\displaystyle x^{2} - x - 6$"
      ],
      "text/plain": [
       " 2        \n",
       "x  - x - 6"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "expand((x + 2)*(x - 3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAABkAAAAOCAYAAADaOrdAAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAA/ElEQVQ4EbWU0Q2CMBRFqXEA4giwAeoGuoGMoG6gn/CLG+gKjiCuwAY6AroBnksgaRC/Wm/S3PJqzoW+VtM0TeCqPM8jGMeOs8BrPVOvVDOuIV1AgacCSswL7MBYMy8nKjpKwK3NAKyvejOuqvsIWcF5AQ4FtFQyD6lHPkIEewLTm48pdO7JGFU1Qh9YhBsfX/KVAzhRAKM9cX8JAa6GXwg74YHJskwNuzOGjdP6L6UA2jsw/AH1s2r4vl/z2hPAO8BzO0BB0z7N1QFvYMR2AHP1xcs9CYCp0Uu8/2sRW1Jw7bxd3dvegOm+2JrxkLAe+9gunSRti/oxVHs4PtWUVA9EJHALAAAAAElFTkSuQmCC\n",
      "text/latex": [
       "$\\displaystyle -2$"
      ],
      "text/plain": [
       "-2"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "expand((x + 1)*(x - 2) - (x - 1)*x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### factor\n",
    "\n",
    "`factor()` 与 `expand()` 功能相反 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAIoAAAAaCAYAAABo4cQnAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAE5klEQVRoBe2a7VHcMBCGD4YCCOngSjhIB9ABkAqADmD4Bf8y0AGkggQ6gFTARwdQArkOyPsIyePTyT7JH3f2hZ3Zky1Lq31Xq9VavpX39/fBJ7VngfPz83VJP7UjDG15oPpxe6M2L3m1eZHLJVETuiveroHqQv1PLO9Jzpv4qYa8VrtKz4vQAJ+OErKKrZPRdnW5o/K+pNmsR4fqn3c0JmKoutGsjgt6/ku63fljTzmKGrnw6Lft1X1dHOpvtgyVRzWB0/+xpoy5dRfeZw12o/I4P+iEo9iHXfX0vN4x16zaCbAxnXJtbnR9lbuvdCkdrsX5fATHeVUdE9JJQmcpdqSSxWIocxRVEma/qry1zzpfSNeR+EWcAXJKq85sFyrBlUTqQ1TdUonBGiPJYxGiz2ZjQisIQg9x0G45cSySn+5+jQt1cmF2oQCcUmWl1RUAJIVb4sKtUm0vxU/ie3F+VZcNwbMTcdNOgp7kJ5uJuqhLfdKYzHGU3exo4CcRX0df4yhUiGuHWTtAq4U1Mm8PA12ztczaKsEFvpRc41Dtd8SNkPQ0TqLSyFSJzmOVr6kDqA+JMdtqkiOrPQsl2m60F7M97ouvV/UD7acO/NGt+78WF/hYUTNJ7ZzjNZKASh5OgrP+QLaVj9MSEasQOKKwVBHu9cEGxrnWpDgemuzZnsCu34LPrIwIRY09ZBdWYBPEmQkTO3F2IvkpEa4JParIeFEn7DZg68FjCs8JBAiQhGLom/hAzCr5LoYe1KbrCTD4CPsx4RqMpU6SYhO1/SJ5fSW2HnKUdbYeEkI8p4hIaEgKL9XgQUxCtK17Ej6ciP2/6wQ+nDuGwDRrW1gGm8TYwtlhiKMUGkbOQCTxHYHXOxdBNgLPVdU5AjA4YwhM46KGS2STIoj5emeHDbaeMsM8yjD5/IXw/ezqVJpEJy/Zv1YbJuiPOHaiELGnfoS9pggMTUWU2jaJASX8JMDkSz4xXwM9D+U4zM3MOfEFlty7iGJylMJ2GtSfLBRny4kmycArF30+U7YYfCzoaybDf8B9EzYJyfXrNE7IERifiM7rMalA2+Ts8MbWw2qbudqlmPNut+2gtEl02ta2Afngy1ZHA/KMiJ7bJMYMzi/MgRsGnArLOIHq+d5B4sZbg/FwXee3olPdJ0UYyVkEsTLyepfpULhwlswmZTZwzyYiCtsLr4Q+EUHgN2sgHGrsGqmOZ7wF9YHY+u4iFQUTb4IhWiabhPD5dQQQTmhNRMGAoeN7ogjnDhhnoMZ8TbwT05bDIxwo24ZoMy/SuEQ6Ip6bUL7n4PCc6YT2bjD4b2+qChJyzJYqWdnCsC07a5MgEq+ygt1YYGAeDPgr5NnZ2Yt4xPWysXANwZeCS+3/irdT+syrrfTaFR/PYzyN8yQ+ZCySWSj1o9lHr378kkPFRhOH6LcuzAc8V9GhkijnR7rG1VP0IWKPxNjiw1FUyRbDK9dUUkujvpLFk/ylVXjZXt1ni07BFyb+MhHzKaKu3nzjudVYxildREEoBzWhXIVnfSXwJB9AyTjkKUwIZxb/KxGJszfazFGs55CwdnIlpc6WxQGeqmGaj5+nqeMuQ3truyuV2ZHCConKJ4UtIEMRUfgXfvCUNNyr37XCSvqBk0zkaFlE6Te8drSXsXj950jAHBG0M0rnpLIoprbrf1BgXfyZnYY0AAAAAElFTkSuQmCC\n",
      "text/latex": [
       "$\\displaystyle \\left(x - 1\\right) \\left(x^{2} + 1\\right)$"
      ],
      "text/plain": [
       "        ⎛ 2    ⎞\n",
       "(x - 1)⋅⎝x  + 1⎠"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "factor(x**3 - x**2 + x - 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAGEAAAAaCAYAAACn4zKhAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAEy0lEQVRoBe2Z2XEUMRBAjcsBcESAnYHBEWBnwBEBkAEuf9l/lMkAiICCDAwRcGRgMgA2A/OekFRaMcfO7syWKbartNK0pO5Wn5rZG1dXV1sbmF4DZ2dnN+FyEjntxv4p+NnO9Ow3HKIGzlH486QNxq8Zf6XtbSfkpp9cA89Q/GHB5ZzxLrj9jREKrUw8NAq+NPG4sakJTWqZHkcEGAkP6cdPRxBNRWf6k1wjDkPOzdp9DUC75xFGTUcQfwFNGfyPYH73/J0QjWUU3GM8c/Fo6QiCWvaA/ljC1xmiIpKc95H1J+0Y/LdV5Ga/RvhO/6GJTuTrLemR8/Q67GwUI0DMO/An+hBeMriugIymy6wI5QSnZ6rAI8YfxS0L7Pfa+YA+eHmiw7N8vZYm4ztlsT4eKx15CBmsDTjUIe3ZEgyV9Wm5DzoqRqW9L/FLjtWDPGrQOF5R7VPz2jpaJPyC2K2a65TP8DP9mYdfDeHD+l+sN3JvMc7eylgDSHOP8Xf6pSHyuFvS7yK2ciTASOuuJHSXgBPMmW7M29kAFQ8NtCqoj8eLEpn7bIFgFooUko94/kYzlxn6b1qIWmRa8yj7PFRKGweMTQXSfEITPrOmsZD9mR73F16hKDZQDbc65kNxLuTeY+0lzzni4txb+jZa6uOI1qazOfZ1JLxl1pymUixeFitvDV3EvF1c0tpAOq9sLPhMk4dGNQ9roKb8CXp9gCwaQMcoi+ZJlPkC/EkljV5u6moD9SG9hSBHAgzdNFfVwanA/NGphaKK9Ir3F7DXCKiVrPDpsLcb5v+iswaE0f8GeYO306sLHUbQo+t0K641+plTHwuntdIImRFCJA9NyoJmK6jIWcvsF2hluqxReFNcwNG3hXMmxxoj07pTg3y3mG9yEnn00o77pf+xovOT55Qi9fqXri1AeWpcMR2MNjwSEgWYu/l5JVSaHtRDo375UfhFDJv5tMkB3ogafDvKhBlAI9SqmgfPwanolVeHzOkYnKlLXFckdDkmW+dhu3yMTP2olBXFWIZdoFf3rdmKtKWTPEzcTVsX8anm4KsRvY7mSGKsUUsPNpqMqlkhh4aZgasdrFgS9NGYostFaZyNAFGJN3mWhbQLZFYKHtZCTwVfRLriwmF5LtOTxa88YNg79Q889eamTywaplSe5yrlVbS+euAaI6HeJ74RdsRGofR+re5twHaHplB9uVWPOKDVoFFt3q70dg+XFQ7OuVT8GK4H4KtiLcTWAetBAhW3Dy5fRXlWkeIDRJmVO0dPnKo7P9+ow4VgJ656AgMtvEWv4vV+BbA2qOQukFl5mLTWnGkuVWjpSsvIcK2v7WXxc8m6QANoiPTuUvKtz6pj+j7gDe8HzXcGoaseOO+Z61uh+GbwT51V2+np6SVtf1U6Q/bD7yHtxZA9q66F3zntaxcd5ndpl11r6rntZtMMxmr1vhAdTLRng6ktp7eetYOnjViaERuAsSnV6Hn5B9P6a/QsHgUsHsUICGjaqW8WrVKOMQFPc3q+Oo5Bs6JhSnlX4EzRvtDl210xF4bMmebUwyC5Uk2o6S3zbAE334basgyBa7ZHj1ahfrqxFhgZfbXAetd3kWHJPIzyp04iGT3hcKgnpP3/cs+ZTVVG58JX03Te3zxNx5woiDQDAAAAAElFTkSuQmCC\n",
      "text/latex": [
       "$\\displaystyle z \\left(x + 2 y\\right)^{2}$"
      ],
      "text/plain": [
       "           2\n",
       "z⋅(x + 2⋅y) "
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "factor(x**2*z + 4*x*y*z + 4*y**2*z)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`factor_list` 会返回一个因式分解后的列表, 这个列表包含所有的因式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAOAAAAAVCAYAAABfcuJLAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAHDElEQVR4Ad2b7XEURxCGzyoFIOMMRAbCisAiAwERABnIxS/pHwUZABFQOANwBFjKQGRgrAzk95mb2ZrrnZm9/Zjdha6am+/ud7p7Pu9uc39/v4nD5eXlcZyfMy3ZFwq3PpykZC+JL4XHlq0B3xowWL38TPmSflWX9eFUv4NNRFdXVxfKnkRFcyd/k8CXwvFQ4cYKXwE+CymVP/Y4U3XVy34QHVXXQ2UBWRtL/28VHkr+XwoPDI5Wv19YmSB1Old0qvhPV2A+VM7E/KTwSOk7Uz1JVnzfiNFnxV8sQ5UV8dn2NfPCUtSF6lnIvinGCLPRmnTUNWhhPVab4Gu/K/2dvMpbC28Xrynr98WldkUbqz7py7afm4AqPNIg/lb8KB6ML/+gMpSDknC8X1U+6wTM4ROW2aivLtT+WuD+qKUrO/A16MhiyuWFlcn3RvGT0EZpHBanfqx0awEO7WrGfXGpfdbGfjy5zaTpF46gDP6dHZyY3Ck8UXipuo+2fsZ8Et+M8jcDdIE+wT0Xza4j6eRM4cWAAYL1edxPfNgNWdg5ZS1FfXENtXHTL0zAp1LA+6VGvYfcteNrDcHrE9ycLuagJXTE2IaM70z9/kvohp3vSOXskEtQL1xDbRz3O1AGod+WGO0+MteOr2MM6PVpR5vR1T+gjpho3JHZ8VI0ZFKn+PQtG4JrqI1dv0Mh5By+yJl7T+104pMhw6MILDky3yiwinJEWnJnR6+PFWpjKOpIOsChw1HxVGmOf+jnmQL0VW1mezCSLPCmCDtuVO8eYhQH3Lwq3ir/NnTydR8U53iFpnvHBV47uAzDoTZ2/Q7EjMeVW8N0Tdl98PFQxLkaJ+Jyz2WeF7Xaji8xRUKvOHpt6tIROuF5HAf+qoC+WJy4d+Hkc95VJa5NwoKTo6vwMkqjVx7zZ9IURMTJ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PvgzO9XcH6cUi3ngof3GthtMhZue33b7ZNnB+La0odxbhKaWxdw95OpnyJnZp/DoT8kRLsmK+3JdnPsbrMMjYVfW0cujf93ATUIDmm2Nen0HivWDxQzr97Ne7ZaAp8iKyJMR6S5HC8Qp/N03lcb9NT4PKyRtnQ4urKSyZ3uL3G2MUrU88x7WNU90FpvqzPHvlUV80PIxwbyell49DX9jsMFYo5W3MG5oVpCPFr/PjyPIRHqc9YfPCujTHg5z4a7iqhrBRPhWsKHZVwzl3HTsGiwnsDvwdlR2zd/QyoqXRp2LayfW0cGOz0a/4RT60Gx6w+U1xzVQtAWrHkcoziIg49Ud4+pCyKbwur/CnMHJHYGezTebnjRLWSu3odTTTUxdiUbKw6Fguuc7zc4sPNgpHq9z+/wACZxupcgwAAAABJRU5ErkJggg==\n",
      "text/latex": [
       "$\\displaystyle \\left( 1, \\  \\left[ \\left( z, \\  1\\right), \\  \\left( x + 2 y, \\  2\\right)\\right]\\right)$"
      ],
      "text/plain": [
       "(1, [(z, 1), (x + 2⋅y, 2)])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "factor_list(x**2*z + 4*x*y*z + 4*y**2*z)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
